package io.github.talelin.latticy.controller.api;

import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.latticy.bo.PageCounterBO;
import io.github.talelin.latticy.common.CommonUtil;
import io.github.talelin.latticy.entity.Document;
import io.github.talelin.latticy.service.DocumentService;
import io.github.talelin.latticy.vo.PageResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;

@RestController
@RequestMapping("/api/document")
@Validated
public class DocumentController {

    @Autowired
    private DocumentService documentService;

    @GetMapping("/{id}")
    @LoginRequired
    public Document get(@PathVariable @Positive Long id) {
        return documentService.get(id);
    }

    @GetMapping("/page")
    @LoginRequired
    public PageResponseVO page(@RequestParam(required = false, defaultValue = "0")
                     @Min(value = 0) int start,
                     @RequestParam(required = false, defaultValue = "20")
                     @Min(value = 1) int count) {
        PageCounterBO pageCounter = CommonUtil.converToPageParamter(start, count);
        Page<Document> paging = documentService.getALl(pageCounter.getPage(), pageCounter.getSize());
        return new PageResponseVO<>(paging.getTotalElements(), paging.getContent(), paging.getNumber(), paging.getSize());
    }
}
